// pages/hotel/hotelDetail/book/editUserInfo/editUserInfo.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    occupantsName:'',  
    cardType:'',  
    identificationNumber:'',  
    occupantsPhone:'' ,
    array: ['身份证', '护照'],
    userList:[],
    guests:[],
    id:0,
    objectArray: [
      {
        id: 0,
        name: '身份证'
      },
      {
        id: 1,
        name: '护照'
      }
    ],
    index: 0,
  },
  //获取入住人信息
  getUser(options){
    wx.request({
      url: 'http://localhost:8080/hotel/occupants/'+options.id,
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      success: (resp) => {
        console.log(resp)
        this.setData({
          userList: resp.data.data
        })
        console.log('userList',this.data.userList)
        console.log("getUser",resp.data)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    this.getUser(options);
    // this.updateGuest(options)
    this.setData({
      id:Number(options.id)
    })
    // this.setData({
    //   realName: decodeURIComponent(options.realName),
    //   cardType: decodeURIComponent(options.cardType),
    //   idCard: decodeURIComponent(options.idCard),
    //   phone: decodeURIComponent(options.phone),
    // });
  },
  clearRealName(e) {
    this.setData({ occupantsName: '' });
  },
  clearIdCard(e) {
    this.setData({ identificationNumber: '' });
  },

  clearPhone(e) {
    this.setData({ occupantsPhone: '' });
  },
  // getUserInfo() {
  //   const userInfo = {
  //     realName: '张三',
  //     idCard: '123456789012345678',
  //     phone: '13800138000'
  //   };
  //   return userInfo;
  // },
  toSave(e) {
    const occupantsName = e.detail.value.occupantsName;
    // const cardType = e.detail.value.cardType;
    const identificationNumber = e.detail.value.identificationNumber;
    const occupantsPhone = e.detail.value.occupantsPhone;
    console.log("保存",e.detail.value)
    this.setData({
      occupantsName:e.detail.value.username,
      occupantsPhone:e.detail.value.phone,
      identificationNumber:e.detail.value.IdCard
    })
    // console.log(e.detail.value),
    console.log(this.data.occupantsName),
    console.log(this.data.occupantsPhone),
    console.log(this.data.identificationNumber),
    wx.request({
      url: 'http://localhost:8080/hotel/occupants/',
      method: 'PUT',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {  
        id: this.data.id, // 确保将用户 ID 作为数据的一部分传递  
        occupantsName:this.data.occupantsName,
        occupantsPhone:this.data.occupantsPhone,
        identificationNumber:this.data.identificationNumber
      },
      success: (resp) => {
        // console.log(this.data.occupantsName),
        // console.log(this.data.occupantsPhone),
        // console.log(this.data.identificationNumber),
        // console.log(resp)
        this.setData({
          guests: resp.data.rows
        })
        wx.navigateTo({
          url: '/pages/hotel/hotelDetail/book/book',
        })
         console.log(this.data.guests)
      }
    })
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // const userInfo = this.getUserInfo();
    // this.setData({
    //   realName: userInfo.realName,
    //   idCard: userInfo.idCard,
    //   phone: userInfo.phone
    // });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
// Component({});